<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/10
 * Time: 14:44
 */

namespace app\index\controller;


use app\index\service\BannerService;
use think\Config;
use think\Db;
use think\Exception;
use think\Controller;
use think\Loader;
use think\Request;

class Banner extends Controller
{
    /**
     * @Notes:添加banner
     * @param Request $request
     * @return array
     * @author: LiBaoJin
     * @Time:
     */
    public function add_banner(Request$request){
        $postData = $request->post();
        $validate = Loader::validate('Banner');
        $rule = $validate->check($postData);
        if($rule !== true){
            return ReturnData::error(-1,$validate->getError(),['']);
        }
        try{
            Db::startTrans();
            $qiniuConfig = Config::get('qiniu');
            $insert_data = [
              'business_id'         =>$this->business_id,
                'title'              =>$postData['title'],
                'title_color'        =>$postData['title_color'],
                'banner_picture'    =>$qiniuConfig['domainName'].$postData['banner_picture'],
                'main_picture'      =>$qiniuConfig['domainName'].$postData['main_picture'],
                'background_color' =>$postData['background_color'],
                'product_ids'       =>json_encode($postData['product_ids']),
                'state'             =>0,
                'create_time'      =>date('Y-m-d H:i:s'),
                'update_time'      =>date('Y-m-d H:i:s')
            ];
            $banner_id = Db::table('banner')
                ->insertGetId($insert_data);
            if(!$banner_id){
                return ReturnData::error(-1,'添加banner失败',['']);
            }
           $activity_img_key = array_keys($postData['activity_img']);
            if(in_array('activity_img_bag',$activity_img_key)){
                    $activity_img_bag = [
                      'banner_id'=>  $banner_id,
                      'picture'=>  $qiniuConfig['domainName'].$postData['activity_img']['activity_img_bag'],
                      'type'=>  1,
                    ];
                $activity_img_bag_id= BannerService::banner_pictures($activity_img_bag);
                if(!$activity_img_bag_id){
                    return ReturnData::error(-1,'添加包袋图片失败',['']);
                }
            }
            if(in_array('activity_img_watch',$activity_img_key)){
                $activity_img_bag = [
                    'banner_id'=>  $banner_id,
                    'picture'=>  $qiniuConfig['domainName'].$postData['activity_img']['activity_img_watch'],
                    'type'=>  2,
                ];
                $activity_img_bag_id= BannerService::banner_pictures($activity_img_bag);
                if(!$activity_img_bag_id){
                    return ReturnData::error(-1,'添加手表图片失败',['']);
                }
            }
            if(in_array('activity_img_jewelry',$activity_img_key)){
                $activity_img_bag = [
                    'banner_id'=>  $banner_id,
                    'picture'=>  $qiniuConfig['domainName'].$postData['activity_img']['activity_img_jewelry'],
                    'type'=>  3,
                ];
                $activity_img_bag_id= BannerService::banner_pictures($activity_img_bag);
                if(!$activity_img_bag_id){
                    return ReturnData::error(-1,'添加首饰图片失败',['']);
                }
            }
            if(in_array('activity_ornament',$activity_img_key)){
                $activity_img_bag = [
                    'banner_id'=>  $banner_id,
                    'picture'=>  $qiniuConfig['domainName'].$postData['activity_img']['activity_ornament'],
                    'type'=>  4,
                ];
                $activity_img_bag_id= BannerService::banner_pictures($activity_img_bag);
                if(!$activity_img_bag_id){
                    return ReturnData::error(-1,'添加配饰图片失败',['']);
                }
            }
             Db::commit();
            return ReturnData::ok([$banner_id],'新增活动成功');
        }catch(Exception$e){
            Db::rollback();
            return ReturnData::error(-1,'添加banner失败'.$e,['']);
        }





    }

    public function upda_banner(Request$request){

    }



}